package com.btm.distrb;

import com.btm.distrb.comment.TableComment;
import com.btm.distrb.config.ReadConfigFile;
import com.btm.distrb.model.AllDB;
import com.btm.distrb.model.DBInfo;
import com.btm.distrb.model.Table;
import com.btm.distrb.service.Detection;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class Start {
    public static void main(String[] args) {
        if (0 < args.length) {
            switch (args[0]){
                case "help":
                    help();
                    break;
                case "dt":
                    detection();
                    break;
                case "stc":
                    showTableComment();
                    break;
                default:
                    System.out.println("no this parameter ["+args[0]+"]");
                    System.out.println("You can use 'java -jar distrb help' show all parameter.");
            }
        } else {
            System.out.println("You master specific a param.");
        }

    }

    /**
     * 读取配置文件
     * @return
     */
    private static AllDB getAllDB(){
        AllDB allDB = null;
        try {
            ReadConfigFile configFile = new ReadConfigFile();
            configFile.getConfig(new Start().getClass(), "configs");
            allDB = configFile.getAllDB();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.out.println("distrb config init successfully.");
            return allDB;
        }
    }

    /**
     * 对比表结构
     */
    private static void detection() {
        AllDB allDB = getAllDB();
        System.out.println("distrb start.");
        Detection detection = new Detection(allDB.getPrefix());
        DBInfo stdDBinfo = allDB.getStdDB();
        Map<String, Table> stdTableMap = detection.standardDB(stdDBinfo.getUrl(), stdDBinfo.getUserName(), stdDBinfo.getPassword(), stdDBinfo.getCatalog());
        System.out.println("基准数据库信息加载完毕");
        List<DBInfo> contrastDB = allDB.getContrastDB();
        if (null != contrastDB && 0 < contrastDB.size()) {
            DBInfo[] dbInfos = new DBInfo[contrastDB.size()];
            allDB.getContrastDB().toArray(dbInfos);
            detection.detectionDB(dbInfos, stdTableMap);
        } else {
            System.out.println("distrb no find contrast database.");
        }
        System.out.println("distrb end.");
    }

    /**
     * 查询表注释
     */
    private static void showTableComment(){
        AllDB allDB = getAllDB();
        TableComment tableComment = new TableComment();
        tableComment.showTableComment(allDB);
    }

    /**
     * 显示帮助信息
     */
    private static void help(){
        System.out.println("Only one parameter. If more than one,only read first parameter.\n");
        System.out.println("stc  Show Standard Database table comments. This parameter need a config file.");
        System.out.println("dt   detection table from target database to standard database. This parameter need a config file.");
        System.out.println("");
    }
}
